package com.tpp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tpp.base.mapper.CinemaMapper;
import com.tpp.base.service.AdminCinemaSeatService;
import com.tpp.base.service.AdminCinemaService;
import com.tpp.comm.pojo.TpCinema;
import com.tpp.comm.pojo.TpCinemaSeat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AdminCinemaServiceImpl implements AdminCinemaService {

    @Resource
    private CinemaMapper cinemaMapper;

    @Resource
    private AdminCinemaSeatService adminCinemaSeatService;

    @Override
    public List<TpCinema> findCinema() {
        return cinemaMapper.selectList(null);
    }

    @Override
    @Transactional
    public void addCinema(TpCinema cinema) {
        //先添加剧场
        cinema.setCreatedTime(LocalDateTime.now());
        cinemaMapper.insert(cinema);
        //后添加座位
        int count = cinema.getXLength() * cinema.getYLength();
        TpCinemaSeat[] tpCinemaSeats = new TpCinemaSeat[count];
        for (int x = 0;x< cinema.getXLength();x++){
            for(int y = 0;y<cinema.getYLength();y++){
                int index = x * cinema.getYLength() + y;
                tpCinemaSeats[index]=TpCinemaSeat.builder()
                        .cinemaId(cinema.getId())
                        .X(x+1)
                        .Y(y+1)
                        .sort(index+1)
                        .status(0)
                        .build();
            }
        }
        adminCinemaSeatService.addCinemaSeats(tpCinemaSeats);
    }

    @Override
    public void updateCinema(TpCinema cinema) {
        cinema.setUpdatedTime(LocalDateTime.now());
        cinemaMapper.updateById(cinema);
    }

    @Override
    public void deleteCinema(Integer id) {
        cinemaMapper.deleteById(id);
    }

    @Override
    public TpCinema findById(Integer id) {
        return cinemaMapper.selectById(id);
    }

    @Override
    public List<TpCinema> findByAreaId(Integer areaId) {
        return cinemaMapper.selectList(new QueryWrapper<TpCinema>().lambda().eq(TpCinema::getAreaId,areaId));
    }
}
